home *** CD-ROM | disk | FTP | other *** search
Wrap
function GoogleRedesignedStyle(o) { if (typeof o == "string") { this.node = this.ds.getNode(o); } else if (o) { this.node = o; } else { this.node = this.ds.getAnonymousNode(); } this._description = null; this._enabled = null; this._customized = null; this._code = null; this._originalCode = null; this._global = null; this._domains = null; this._siteURLPrefixes = null; this._siteURLs = null; this._namespaces = null; this._treeDisplayCode = null; this._appliesToDisplay = null; this._namespaceNames = null; this._updateURL = null; this._neverUpdate = null; } GoogleRedesignedStyle.prototype = {containerURI: "urn:googleredesigned:userstyles", descriptionURI: "urn:googleredesigned#description", enabledURI: "urn:googleredesigned#enabled", customizedURI: "urn:googleredesigned#customized", originalCodeURI: "urn:googleredesigned#originalCode", codeURI: "urn:googleredesigned#code", siteURLURI: "urn:googleredesigned#url", updateURLURI: "urn:googleredesigned#updateURL", siteURLPrefixURI: "urn:googleredesigned#urlPrefix", siteDomainURI: "urn:googleredesigned#domain", globalStyleURI: "urn:googleredesigned#global", namespaceURI: "urn:googleredesigned#namespace", neverUpdateURI: "urn:googleredesigned#neverUpdate", documentRulePrefix: "@-moz-document ", copy: function (newStyle) {newStyle.description = this.description;newStyle.customized = this.customized;newStyle.originalCode = this.originalCode;newStyle.code = this.code;newStyle.siteURLs = this.siteURLs;if (this.updateURL) {newStyle.updateURL = this.updateURL;}newStyle.siteURLPrefixes = this.siteURLPrefixes;newStyle.domains = this.domains;newStyle.global = this.global;newStyle.namespaces = this.namespaces;newStyle.neverUpdate = this.neverUpdate;newStyle.enabled = this.enabled;}, applyArray: function (uri, values) {var valueArray;if (values instanceof Array) {valueArray = values;} else {valueArray = [];while (values.hasMoreElements()) {valueArray.push(values.getNext());}}var targets = this.node.getTargets(uri);while (targets.hasMoreElements()) {var target = targets.getNext();this.node.removeTarget(uri, target);}for (var i = 0; i < valueArray.length; i++) {this.node.addTarget(uri, valueArray[i]);}}, applyValue: function (uri, value) {var oldValue = this.node.getTarget(uri);if (oldValue) {this.node.modifyTarget(uri, oldValue, value);} else {this.node.addTarget(uri, value);}}, register: function () {googleredesignedCommon.registerStyle(this.uri, this.code);}, unregister: function () {googleredesignedCommon.unregisterStyle(this.uri, this.code);}, get uri() {return this.node.source.Value;}, get description() {if (this._description) {return this._description;}var descriptionLiteral = this.node.getTarget(this.descriptionURI);this._description = descriptionLiteral != null ? descriptionLiteral.getValue() : "";return this._description;}, set description(description) {this.applyValue(this.descriptionURI, description);this._description = null;}, get enabled() {if (this._enabled != null) {return this._enabled;}var enabledLiteral = this.node.getTarget(this.enabledURI);this._enabled = enabledLiteral != null ? enabledLiteral.getValue() == "true" : false;return this._enabled;}, set enabled(enabled) {var newTarget;if (enabled) {this.register();} else {this.unregister();}this.applyValue(this.enabledURI, enabled ? "true" : "false");this._enabled = null;}, enableWithoutRegister: function () {this.applyValue(this.enabledURI, "true");this._enabled = null;}, get enabledString() {return "" + this.enabled;}, set enabledString(enabledString) {this.enabled = enabledString == "true";}, get customized() {if (this._customized) {return this._customized;}var customizedLiteral = this.node.getTarget(this.customizedURI);this._customized = customizedLiteral != null ? customizedLiteral.getValue() == "true" : false;return this._customized;}, set customized(customized) {this.applyValue(this.customizedURI, customized ? "true" : "false");this._customized = null;}, get code() {if (this._code) {return this._code;}var codeLiteral = this.node.getTarget(this.codeURI);this._code = codeLiteral != null ? codeLiteral.getValue() : "";return this._code;}, set code(code) {this.applyValue(this.codeURI, code);this._code = null;}, get originalCode() {if (this._originalCode) {return this._originalCode;}var originalCodeLiteral = this.node.getTarget(this.originalCodeURI);this._originalCode = originalCodeLiteral != null ? originalCodeLiteral.getValue() : this.code;return this._originalCode;}, set originalCode(originalCode) {this.applyValue(this.originalCodeURI, originalCode);this._originalCode = null;}, get global() {if (this._global != null) {return this._global;}var globalLiteral = this.node.getTarget(this.globalStyleURI);this._global = globalLiteral != null ? globalLiteral.getValue() == "true" : false;return this._global;}, set global(globalStyle) {this.applyValue(this.globalStyleURI, globalStyle ? "true" : "false");this._global = null;}, get domains() {if (this._domains) {return this._domains;}this._domains = this.node.getTargets(this.siteDomainURI);return this._domains;}, set domains(domains) {this.applyArray(this.siteDomainURI, domains);this._domains = null;}, get siteURLPrefixes() {if (this._siteURLPrefixes) {return this._siteURLPrefixes;}this._siteURLPrefixes = this.node.getTargets(this.siteURLPrefixURI);return this._siteURLPrefixes;}, set siteURLPrefixes(prefixes) {this.applyArray(this.siteURLPrefixURI, prefixes);this._siteURLPrefixes = null;}, get siteURLs() {if (this._siteURLs) {return this._siteURLs;}this._siteURLs = this.node.getTargets(this.siteURLURI);return this._siteURLs;}, set siteURLs(urls) {this.applyArray(this.siteURLURI, urls);this._siteURLs = null;}, get namespaces() {if (this._namespaces) {return this._namespaces;}this._namespaces = this.node.getTargets(this.namespaceURI);return this._namespaces;}, set namespaces(namespaces) {this.applyArray(this.namespaceURI, namespaces);this._namespaces = null;}, get treeDisplayCode() {if (this._treeDisplayCode) {return this._treeDisplayCode;}this._treeDisplayCode = this.code.substring(0, 100).replace(/\n/g, " ");return this._treeDisplayCode;}, get appliesToDisplayArray() {if (this.global) {var namespaceNames = this.namespaceNames;if (namespaceNames.length > 0) {return namespaceNames;}return [STRINGS.getString("globalDisplay")];}var components = [];var domains = this.domains;while (domains.hasMoreElements()) {googleredesignedCommon.addAsSet(components, domains.getNext().getValue());}var urlPrefixes = this.siteURLPrefixes;while (urlPrefixes.hasMoreElements()) {googleredesignedCommon.addAsSet(components, this.formatUrlPrefixForDisplay(urlPrefixes.getNext().getValue()));}var urls = this.siteURLs;while (urls.hasMoreElements()) {googleredesignedCommon.addAsSet(components, this.formatUrlForDisplay(urls.getNext().getValue()));}return components;}, getLastModifiedDate: function (passed) {var dsp = new GoogleRedesignedRDFDataSource(googleredesignedBrowserOverlay.getConfigUri());var te = dsp.getNode(passed);var tg = te.getTarget("urn:googleredesigned#lastModifiedDate");return tg.getValue();}, get appliesToDisplay() {if (this._appliesToDisplay) {return this._appliesToDisplay;}this._appliesToDisplay = this.appliesToDisplayArray.join(", ");return this._appliesToDisplay;}, formatUrlPrefixForDisplay: function (urlPrefix) {if (this.isUIUrl(urlPrefix)) {return STRINGS.getString("xulDisplay");}return urlPrefix + "*";}, formatUrlForDisplay: function (url) {if (this.isUIUrl(url)) {return STRINGS.getString("xulDisplay");}return url;}, isUIUrl: function (url) {return /^(chrome|about|x-jsd)/.test(url);}, get namespaceNames() {if (this._namespaceNames) {return this._namespaceNames;}var namespaces = this.namespaces;this._namespaceNames = [];while (namespaces.hasMoreElements()) {var currentNamespace = namespaces.getNext().getValue();switch (currentNamespace) {case googleredesignedCommon.XULNS:this._namespaceNames.push(STRINGS.getString("xulDisplay"));break;case googleredesignedCommon.HTMLNS:this._namespaceNames.push(STRINGS.getString("htmlDisplay"));break;default:this._namespaceNames[this._namespaceNames.length] = currentNamespace.substring(currentNamespace.lastIndexOf("/") + 1);}}return this._namespaceNames;}, getParameterizedURI: function (parameter) {if (this.uri.substring(0, 3) == "urn" || this.uri.substring(0, 3) == "rdf") {return null;}if (this.uri.indexOf("?") > -1) {return this.uri + "&" + parameter;}return this.uri + "?" + parameter;}, get updateURL() {if (this._updateURL) {return this._updateURL;}var updateURL = this.node.getTarget(this.updateURLURI);if (updateURL) {this._updateURL = updateURL.getValue();} else {this._updateURL = this.getParameterizedURI("raw");}return this._updateURL;}, set updateURL(updateURL) {this.applyValue(this.updateURLURI, updateURL);this._updateURL = null;}, get neverUpdate() {if (this._neverUpdate != null) {return this._neverUpdate;}var neverUpdateLiteral = this.node.getTarget(this.neverUpdateURI);this._neverUpdate = neverUpdateLiteral != null ? neverUpdateLiteral.getValue() == "true" : false;return this._neverUpdate;}, set neverUpdate(neverUpdate) {this.applyValue(this.neverUpdateURI, neverUpdate ? "true" : "false");this._neverUpdate = null;}, checkForUpdate: function (callback) { if (this.neverUpdate) {callback(null);return;} var updateURL = this.updateURL; if (!updateURL) {callback(null);return;} var tgu = this.updateURL; var currentCode = this.originalCode; var currentDescription = this.description; var requestTimer = setTimeout(function () { // clean up if old channel exists. Should never happen really! if(this.gChannel){ this.gChannel.cancel(Components.results.NS_BINDING_ABORTED); this.gChannel=null; } GoogleRedesignedUpdateManager.googleredesignedupdateabortarray.push(currentDescription); callback(null); }, 30000); //listener for the converted data var listener2 ={ jj:"", onDataAvailable : function(request, context, inputStream, offset, count) { var sStream = Components.classes["@mozilla.org/scriptableinputstream;1"].createInstance().QueryInterface(Components.interfaces.nsIScriptableInputStream); sStream.init(inputStream); sStream.available(); while (sStream.available()) { listener2.jj+=(sStream.read(count)); } }, onStartRequest : function(request, context){}, onStopRequest : function(aRequest, context){ clearTimeout(requestTimer); if (aRequest.QueryInterface(Components.interfaces.nsIHttpChannel).responseStatus == 304){ callback(null); return; } if (aRequest.QueryInterface(Components.interfaces.nsIHttpChannel).getResponseHeader("Content-Type").indexOf("application/x-gzip") != 0) { GoogleRedesignedUpdateManager.googleredesignedupdateabortarray.push(currentDescription); callback(null); return; } if (!googleredesignedCommon.cssAreEqual(listener2.jj, currentCode)) { GoogleRedesignedUpdateManager.googleredesignedupdatesuccessarray.push(currentDescription); var dsp = new GoogleRedesignedRDFDataSource(googleredesignedBrowserOverlay.getConfigUri()); var te = dsp.getNode(tgu); var tg = te.getTarget("urn:googleredesigned#lastModifiedDate"); var tm = tg.getValue(); te.modifyTarget("urn:googleredesigned#lastModifiedDate", tm, aRequest.QueryInterface(Components.interfaces.nsIHttpChannel).getResponseHeader("Last-Modified")); dsp.save(); callback(listener2.jj); return; } callback(null); } }; var conv; function StreamListener(aCallbackFunc) { this.mCallbackFunc = aCallbackFunc; } StreamListener.prototype = { mData: "", // nsIStreamListener onStartRequest: function (aRequest, aContext) { this.mData = ""; //this.mCallbackFunc("started"); // Attempt to gunzip conv = Components.classes["@mozilla.org/streamconv;1?from=gzip&to=uncompressed"].createInstance(Components.interfaces.nsIStreamConverter); conv.asyncConvertData("gzip", "uncompressed", listener2, aRequest); conv.onStartRequest(aRequest, aContext); }, onDataAvailable: function (aRequest, aContext, aStream, aSourceOffset, aLength) { //really do the conversion conv.onDataAvailable(aRequest, aContext, aStream, aSourceOffset, aLength); }, onStopRequest: function (aRequest, aContext, aStatus) { if (Components.isSuccessCode(aStatus)) { // request was successfull // this.mCallbackFunc(aRequest); //result //this.mCallbackFunc(this.mData); var status = {}; conv.onStopRequest(aRequest, aContext, aStatus); } else { // request failed //this.mCallbackFunc(aStatus); clearTimeout(requestTimer); GoogleRedesignedUpdateManager.googleredesignedupdateabortarray.push(currentDescription); callback(null); } this.gChannel = null; }, // nsIChannelEventSink onChannelRedirect: function (aOldChannel, aNewChannel, aFlags) { // if redirecting, store the new channel gChannel = aNewChannel; //this.mCallbackFunc(aNewChannel.name); }, // nsIInterfaceRequestor getInterface: function (aIID) { try { return this.QueryInterface(aIID); } catch (e) { throw Components.results.NS_NOINTERFACE; } }, // nsIProgressEventSink (not implementing will cause annoying exceptions) onProgress : function (aRequest, aContext, aProgress, aProgressMax) { }, onStatus : function (aRequest, aContext, aStatus, aStatusArg) { }, // nsIHttpEventSink (not implementing will cause annoying exceptions) onRedirect : function (aOldChannel, aNewChannel) { }, // we are faking an XPCOM interface, so we need to implement QI QueryInterface : function(aIID) { if (aIID.equals(Components.interfaces.nsISupports) || aIID.equals(Components.interfaces.nsIInterfaceRequestor) || aIID.equals(Components.interfaces.nsIChannelEventSink) || aIID.equals(Components.interfaces.nsIProgressEventSink) || aIID.equals(Components.interfaces.nsIHttpEventSink) || aIID.equals(Components.interfaces.nsIStreamListener)) return this; throw Components.results.NS_NOINTERFACE; } }; // global channel if(this.gChannel){ this.gChannel.cancel(Components.results.NS_BINDING_ABORTED); this.gChannel = null; } // init the channel // the IO service var ioService = Components.classes["@mozilla.org/network/io-service;1"].getService(Components.interfaces.nsIIOService); // create an nsIURI var uri = ioService.newURI(updateURL += (updateURL.match(/\?/) == null ? "?" : "&") + (new Date).getTime(), null, null); // get a channel for that nsIURI this.gChannel = ioService.newChannelFromURI(uri); // get an listener var callbackFunc=function(g){alert(g)} var listener = new StreamListener(callbackFunc); this.gChannel.QueryInterface(Components.interfaces.nsIHttpChannel).setRequestHeader("If-Modified-Since",this.getLastModifiedDate(this.updateURL),false); this.gChannel.notificationCallbacks = listener; try { this.gChannel.asyncOpen(listener, null); } catch (ex) { alert(updateURL); callback(null); return; } }, gChannel:null, save: function () {this.ds.getNode(this.containerURI).addChild(this.node);this.ds.save();}, getDatasourceURI: function () {var prefs = Components.classes['@mozilla.org/preferences-service;1'].getService(Components.interfaces.nsIPrefBranch);var prefPath = prefs.getCharPref("extensions.googleredesigned.fileURL");if (prefPath.length > 0) {return prefPath;}var file = Components.classes['@mozilla.org/file/directory_service;1'].getService(Components.interfaces.nsIProperties).get("ProfD", Components.interfaces.nsIFile);file.append("extensions");file.append("{cc85cd4e-5a5b-4eda-a25c-bdaffa93b406}");file.append("chrome");file.append("content");file.append("googleredesigned.rdf");var ioService = Components.classes['@mozilla.org/network/io-service;1'].getService(Components.interfaces.nsIIOService);if (!file.exists()) {var scriptableStream = Components.classes['@mozilla.org/scriptableinputstream;1'].getService(Components.interfaces.nsIScriptableInputStream);var channel = ioService.newChannel("chrome://googleredesigned/content/googleredesigned-default.rdf", null, null);var input = channel.open();scriptableStream.init(input);var data = scriptableStream.read(input.available());scriptableStream.close();input.close();var foStream = Components.classes['@mozilla.org/network/file-output-stream;1'].createInstance(Components.interfaces.nsIFileOutputStream);foStream.init(file, 2 | 8 | 32, 436, 0);foStream.write(data, data.length);foStream.close();}return ioService.newFileURI(file).spec;}, calculateMetadata: function (stylesheet) {var domains = [];var urlPrefixes = [];var urls = [];var namespaces = [];var isGlobal = false;for (var i = 0; i < stylesheet.cssRules.length; i++) {var rule = stylesheet.cssRules[i];var isDocRule;try {rule.QueryInterface(Components.interfaces.nsIDOMCSSMozDocumentRule);isDocRule = true;} catch (ex) {if (ex.name == "NS_NOINTERFACE") {isDocRule = false;if (rule.type == Components.interfaces.nsIDOMCSSRule.STYLE_RULE) {isGlobal = true;} else if (rule.type == Components.interfaces.nsIDOMCSSRule.UNKNOWN_RULE) {if (rule.cssText.indexOf("@namespace") == 0) {var start = rule.cssText.indexOf("url(");var end = rule.cssText.lastIndexOf(")");namespaces[namespaces.length] = rule.cssText.substring(start + 4, end);}}} else {throw ex;}}if (isDocRule) {var mozDocPosition = rule.cssText.indexOf(GoogleRedesignedStyle.prototype.documentRulePrefix);if (mozDocPosition == -1) {alert("Rule QIs to moz-document but moz-document string not found.");continue;}var mozDocEnd = rule.cssText.indexOf(" {");var sitesString = rule.cssText.substring(mozDocPosition + GoogleRedesignedStyle.prototype.documentRulePrefix.length, mozDocEnd - 1);var sites = sitesString.split(", ");for (var j = 0; j < sites.length; j++) {var openParenthesis = sites[j].indexOf("(\"");var site = sites[j].substring(openParenthesis + 2, sites[j].length - 2);var type = sites[j].substring(0, openParenthesis);switch (type) {case "url":urls[urls.length] = site;break;case "url-prefix":if (site.indexOf(":") == -1 || /^(^\/)*:\/?\/?$/.test(site)) {isGlobal = true;} else {urlPrefixes[urlPrefixes.length] = site;}break;case "domain":domains[domains.length] = site;break;default:alert("Unrecognized site rule type '" + type + "'.");}}}}this.global = isGlobal;this.namespaces = namespaces;this.domains = domains;this.siteURLs = urls;this.siteURLPrefixes = urlPrefixes;}, appliesToNamespace: function (namespace) {var styleNamespaces = this.namespaces;if (!styleNamespaces.hasMoreElements()) {return true;}while (styleNamespaces.hasMoreElements()) {if (styleNamespaces.getNext().getValue() == namespace) {return true;}}return false;}}; GoogleRedesignedStyle.prototype.ds = new GoogleRedesignedRDFDataSource(GoogleRedesignedStyle.prototype.getDatasourceURI()); var loadedListener; function GoogleRedesignedStylesheetLoadedListener(doc, errorListener, callback) { loadedListener = this; this.doc = doc; this.errorListener = errorListener; this.callback = callback; } GoogleRedesignedStylesheetLoadedListener.prototype = {checkStyleLoaded: function () {try {try {var stylesheet = loadedListener.doc.QueryInterface(Components.interfaces.nsIDOMDocumentStyle).styleSheets[0];stylesheet.cssRules.length;} catch (ex) {if (ex.name == "NS_ERROR_DOM_INVALID_ACCESS_ERR") {setTimeout(loadedListener.checkStyleLoaded, 100);} else {loadedListener.unregisterErrorListener();var data = {exception: ex, stylesheet: stylesheet, errors: loadedListener.errorListener.errors};loadedListener.callback(false, data);loadedListener.destroy();}return;}loadedListener.unregisterErrorListener();if (loadedListener.errorListener) {loadedListener.callback(true, {stylesheet: stylesheet, errors: loadedListener.errorListener.errors});} else {loadedListener.callback(true, {stylesheet: stylesheet, errors: []});}} catch (ex) {Components.utils.reportError(ex);alert(ex);}}, destroy: function () {loadedListener = null;}, unregisterErrorListener: function () {var consoleService = Components.classes['@mozilla.org/consoleservice;1'].getService(Components.interfaces.nsIConsoleService);consoleService.unregisterListener(loadedListener.errorListener);}};